<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500px" :append-to-body="true" :before-close="handleClose">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm" status-icon>
      <el-form-item label="姓名" prop="staff">
        <el-input placeholder="请输入" v-model="ruleForm.staff" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input placeholder="请输入" v-model="ruleForm.phone" />
      </el-form-item>
      <el-form-item label="邮政账号" prop="postalAccount">
        <el-input placeholder="请输入" v-model="ruleForm.postalAccount" />
      </el-form-item>
      <el-form-item label="开户行" prop="accountName">
        <el-input placeholder="请输入" v-model="ruleForm.accountName" />
      </el-form-item>
      <el-form-item label="车主" prop="carOwner">
        <el-input placeholder="请输入" v-model="ruleForm.carOwner" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input placeholder="请输入" type="textarea" v-model="ruleForm.address" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idNumber">
        <el-input placeholder="请输入" v-model="ruleForm.idNumber" />
      </el-form-item>
      <el-form-item label="车牌号" prop="vehicleNumber">
        <el-input placeholder="请输入" v-model="ruleForm.vehicleNumber" />
      </el-form-item>
      <el-form-item label="车长" prop="vehicleCommander">
        <el-input placeholder="请输入" v-model="ruleForm.vehicleCommander" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCloses(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="submit(ruleFormRef)">
          确 认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import { REQUEST } from '@/api/request'
const ruleFormRef = ref<FormInstance>()
const title = ref('')
import { store } from '@/store'
const dialogVisible = ref(false)
const emits = defineEmits(['ok'])
const ruleForm = ref({
  id: '',
  staff: '',//姓名
  phone: '',//手机号
  postalAccount:'', //邮政账号
  accountName:'', //开户行
  carOwner:'', //车主
  address:'', //地址
  idNumber:'', //身份证号
  vehicleNumber:'', // 车牌号
  vehicleCommander:'', //车长
})
const options = ref([])
const rules = ref({
  staff: [{ required: true, message: '请输入姓名', trigger: 'change' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'change' }],
  password: [{ required: true, message: '请输入密码', trigger: 'change' }],
  houseId: [{ required: true, message: '请选择仓库', trigger: 'change' }],
  isDriver: [{ required: true, message: '请选择角色', trigger: 'change' }],
  plateNumber: [{ required: true, message: '请输入车牌号', trigger: 'change' }]
})
const openModal = (t: any, data: any, e: any) => {
  options.value = data
  ruleForm.value = {
    id: '',
    staff: '',
    phone: '',
    houseId: '',
    isDriver: 0,
    plateNumber: ''
  }
  dialogVisible.value = true
  title.value = t
  if (e) {
    ruleForm.value = e
  }
}
const handleClose = () => {
  dialogVisible.value = false
  emits('ok')
}
const handleCloses = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  emits('ok')
  dialogVisible.value = false
}
const submit = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {

        REQUEST.POST('STAFFEDIT', ruleForm.value).then((res: any) => {
          if (res.status === 200) {
            ElMessage({
              type: 'success',
              message: '编辑成功！'
            })
            dialogVisible.value = false
            formEl.resetFields()
            emits('ok')
          } else {
            ElMessage({
              type: 'warning',
              message: res.message
            })
          }
        })

      }
  })
}
defineExpose({ openModal })
</script>

<style lang="" scoped></style>
